/**
 * 
 */
package com.wolfpire.knowledge.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.SubjectKnowledgeRelation;

/**
 * @author lihd
 *
 */

@Repository("subjectKnowledgeRelationDao")
public class SubjectKnowledgeRelationDaoImpl extends BaseHibernateDao<SubjectKnowledgeRelation, Long> implements SubjectKnowledgeRelationDao {

	public void relate(Long subjectId, Long knowledgeId) {
		//先删除后绑定
		this.deleteByKnowledgeId(knowledgeId);
		if (null != subjectId) {
			SubjectKnowledgeRelation sk = new SubjectKnowledgeRelation(knowledgeId,subjectId);
			this.saveOrUpdate(sk);
		}
	}
		
	private void deleteByKnowledgeId(Long knowledgeId) {
		String executeSql = "DELETE FROM t_subject_knowledge WHERE knowledge_id = ?";
		this.executeSql(executeSql, knowledgeId);
			
	}

	public SubjectKnowledgeRelation getSKRelation(Long knowledgeId) {
		Criteria c = createCriteria(Restrictions.eq("knowledgeId", knowledgeId));
		Object obj = c.uniqueResult();
		if (null != obj) {
			return (SubjectKnowledgeRelation) obj;
		}
		return null;
	}

	public List<SubjectKnowledgeRelation> getKSRelations(Long subjectId) {
		Criteria c = createCriteria(Restrictions.eq("subjectId", subjectId), Restrictions.isNotNull("knowledgeId"));
		return c.list();
	}


}
